AP_Scripting: adjust string metatable setup to fix sandbox integrity #27666
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In Lua, strings are the only type that come with a default metatable. The metatable must be shared by all string objects, and it is set to be the
string
library table each time that library is opened. In Ardupilot's scripting engine, the last script to load then has access to the string metatable as the library is opened fresh for each script, as itsstring
library will have been set to the metatable.Therefore, if two scripts are loaded, A first and B second, and script B executes e.g.
string.byte = "haha"
, thenstring.byte()
ands:byte()
for script B are broken. Because the metatable is shared, this also breakss:byte()
for script A, which violates the integrity of the sandbox.Fix the issue by disabling the metatable setup functionality when the string libary is opened, then manually opening an additional copy of the library (which won't be given to any script) and setting it as the string metatable during intialization.
This will break any script that modifies the string metatable for constructive purposes, but such a script could have been broken if it weren't the only script running anyway.
Tested on SITL and Cube Orange with my REPL. Saves 24 bytes of flash on Cube Orange, but costs 770 bytes of Lua state RAM, which I do not love.